/**
 * Created by Lwang on 2017-03-30.
 */
import Util from "utils"
import $ from "jquery";

var servicerCtrl = ['$scope', '$stateParams', '$state', '$location', '$http', '$timeout', '$rootScope', 'Http',function ($scope, $stateParams, $state, $location, $http, $timeout, $rootScope, Http) {
    var bool = true;
    var totalArr = [];
    var totalPage = 0;
    var page = 0;
    $scope.servicers = "";

    //加载更多
    $(window).scroll(function () {
        if($state.current.name == "servicer"){
            var dh = $(document).height(),
                wh = $(window).height(),
                scrollTop = $(window).scrollTop();
            if (dh == wh + scrollTop) {
                if (page < totalPage) {
                    if (bool) {
                        bool = false;
                        page++;
                        serviceAjax({page: page});
                    }
                } else {
                    // Util.tips({
                    //     content: "已经到达最后一页!!!"
                    // })
                }
            }
        }

    });
    //初始化
    serviceAjax({});

    // //联系我
    // $scope.contactMe = function (phone) {
    //     layer.open({
    //         type: 1,
    //         skin: "footer",
    //         content: phone,
    //         btn: ["取消", "拨打号码"],
    //         yes: function (index) {
    //             layer.close(index)
    //         },
    //         no: function (elem) {
    //             location.href = "tel:" + phone
    //         }
    //     })
    // };
    function serviceAjax({page = 0, size = 10}) {

        Http.request({
            url: Util.ajax.customerService + "?page=" + page + "&size=" + size,
            method: "get",
            success: function (res) {
                Http.handler(Util.merge(res, {
                    success: function (data) {
                        totalPage = data.totalPages;
                        data.content.map((_data, index) => {
                            if (_data.starLevel > 5) {
                                _data.starLevel = 5;
                            }
                            _data.starNum = Array.from({length: _data.starLevel});
                            totalArr.push(_data);
                        });
                        $scope.servicers = totalArr;
                        bool = true;
                    },
                    error: function (err) {

                    }
                }))
            }
        })
    }

}];
export {servicerCtrl};